//
//  DataPickView.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/6.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "DataPickView.h"

@interface DataPickView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,weak)UIDatePicker *dataPick;
@property (nonatomic,weak)UIPickerView *pickView;

@property (nonatomic,weak)UIButton *CancalBtn;
@property (nonatomic,weak)UIButton *SaveBtn;
@property (nonatomic,strong)UIView *bgView;



@end

@implementation DataPickView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    UIButton *CancalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CancalBtn.backgroundColor = KOrange;
    [CancalBtn setTitle:@"取消" forState:UIControlStateNormal];
    [CancalBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [CancalBtn addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
    CancalBtn.layer.cornerRadius = 3 ;
    CancalBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:CancalBtn];
    _CancalBtn = CancalBtn;
    
    UIButton *SaveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    SaveBtn.backgroundColor = KOrange;
    SaveBtn.layer.cornerRadius = 3 ;
    [SaveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [SaveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [SaveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    SaveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:SaveBtn];
    _SaveBtn = SaveBtn;
    
    __weak __typeof(&*self)weakSelf = self;
    
    [CancalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_centerX).offset(-8);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-12);
        make.size.mas_equalTo(CGSizeMake(95, 40));
    }];
    [SaveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_centerX).offset(8);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-12);
        make.size.mas_equalTo(CGSizeMake(95, 40));
    }];
    
}
- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
}
- (void)setDataType:(NSInteger)dataType{
    _dataType = dataType;
}
- (void)setTypes:(NSString *)types{
    _types = types;
     __weak __typeof(&*self)weakSelf = self;
    if ([_types isEqualToString:@"date"]) {
        [self addSubview:self.dataPick];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        //    if (_SeleRowStr.length > 0) {
        //        [self.dataPick setDate:[dateFormatter dateFromString:_SeleRowStr] animated:YES];
        //    }
        [self.dataPick mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf);
            make.bottom.equalTo(weakSelf.SaveBtn.mas_top);
        }];
    }else if([_types isEqualToString:@"normal"]){
        [self addSubview:self.pickView];
        [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf);
            make.bottom.equalTo(weakSelf.SaveBtn.mas_top);
        }];
    }

}

#pragma mark - Animated
- (void)show{
    
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor darkGrayColor];
    _bgView.frame = keywindow.frame;
    _bgView.alpha = 0.6;
    [keywindow addSubview:_bgView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [_bgView addGestureRecognizer:tap];
    
    [keywindow addSubview:self];
    [self animatedIn];
    
}
- (void)save{
    
    if (_dataArr.count) {
        NSDictionary *dic = [_dataArr objectAtIndex:[_pickView selectedRowInComponent:0]];
        self.Gclass(dic);
    }else{
       
        if ([_btnStr isEqualToString:@"start"]) {
            
            NSDate *date = [HWCommon timeStart:[_dataPick date]timeStr:@"00:00:00"];
            self.voice([HWCommon getDateStringWithDate:date DateFormat:@"yyyy-MM-dd HH:mm:ss"]);
        }else if([_btnStr isEqualToString:@"end"]){
            NSDate *date = [HWCommon timeStart:[_dataPick date]timeStr:@"23:59:59"];
            self.voice([HWCommon getDateStringWithDate:date DateFormat:@"yyyy-MM-dd HH:mm:ss"]);
        }else{
            self.voice([HWCommon getDateStringWithDate:[_dataPick date] DateFormat:@"yyyy-MM-dd HH:mm:ss"]);
        }
    }
    [self animatedOut];

}
- (void)tap{
//    if ([self.delegate respondsToSelector:@selector(InformAnimatedOut)]) {
//        [self.delegate InformAnimatedOut];
//    }
    [self animatedOut];
}
- (void)animatedIn
{
    __weak __typeof(&*self)weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
    } completion:^(BOOL finished) {
    }];
}
- (void)animatedOut
{
    __weak __typeof(&*self)weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [weakSelf.bgView removeFromSuperview];
        [weakSelf removeFromSuperview];
    }];
}




#pragma mark - pickerView delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return _dataArr.count;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.minimumScaleFactor = 8.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    if (_dataType == 1) {
        pickerLabel.text=  _dataArr[row][@"gc_name"];
    }else if(_dataType == 2){
        pickerLabel.text=  _dataArr[row][@"bd_name"];
    }
    return pickerLabel;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}

#pragma - Set Get
- (UIPickerView *)pickView{
    if (_pickView == nil) {
        UIPickerView *pickView = [[UIPickerView alloc] init];
        pickView.backgroundColor = [UIColor whiteColor];
        pickView.delegate = self;
        pickView.dataSource = self;
        [self addSubview:pickView];
        _pickView = pickView;
    }
    return _pickView;
}

- (UIDatePicker *)dataPick{
    if (_dataPick == nil) {
        UIDatePicker *pick = [[UIDatePicker alloc] init];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:-100];
        [pick setMaximumDate:[NSDate date]];
        [pick setMinimumDate:[calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]];
        pick.datePickerMode = UIDatePickerModeDate;
        pick.backgroundColor = [UIColor whiteColor];
        [self addSubview:pick];
        _dataPick = pick;
    }
    return _dataPick;
}


@end
